
namespace BiztalkBuildHelper
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    /// <summary>
    /// Solution Parser utility
    /// </summary>
    public static class SolutionParser
    {
        public enum EnumProjType
        {
            btproj,
            csproj,
            vbproj,
            None
        }
        public class Vs2005Project
        {
            public string ProjectName;
            public string Path;
            public EnumProjType ProjectType;
        }

        /// <summary>
        /// Gets list of vs2005 Projects in a given solution
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static List<Vs2005Project> GetListofProjects(string filename)
        {
            List<Vs2005Project> projects = new List<Vs2005Project>(); 

            if (!File.Exists(filename))
            {
                throw new Exception("the solution file does not exist");
            }

            using (StreamReader reader = new StreamReader(filename))
            {
                String line;
                Vs2005Project project = null;

             
                while ((line = reader.ReadLine()) != null)
                {
                    //MatchCollection matches = null;

                    if (line != null && line.StartsWith("Project("))
                    {
                        string[] arrName = line.Split('=');
                        if (arrName == null || arrName.Length < 2)
                            continue;
                        string[] arrPathandName = arrName[1].Split(',');
                        if (arrPathandName == null || arrPathandName.Length < 2)
                            continue;
                        project = new Vs2005Project();
                        project.ProjectName = arrPathandName[0];
                        if (arrPathandName[1] != null )
                            project.Path = arrPathandName[1].Replace("\"", string.Empty).Trim();
                        project.ProjectType = GetProjType(project.Path);   
                              

                        projects.Add(project);

                    }




                }
            }

            return projects;

        }

        public static  EnumProjType GetProjType(string ProjName)
        {
            if (string.IsNullOrEmpty(ProjName))
                return EnumProjType.None;

            
                if (ProjName.EndsWith(EnumProjType.btproj.ToString()   )) 
                    return EnumProjType.btproj ;
                if (ProjName.EndsWith(EnumProjType.csproj.ToString()   )) 
                    return EnumProjType.csproj ;
                if (ProjName.EndsWith(EnumProjType.vbproj.ToString()))
                    return EnumProjType.vbproj;
                else 
                    return EnumProjType.None; 
            }
                
                
 
       

        const string CONST_XPath_ProjCommon = "VisualStudioProject/BIZTALK/Build/Settings/ProjectCommon";
        /// <summary>
        /// Updates Assembly file version, Info version and Assembly version
        /// </summary>
        /// <param name="ProjFileName"></param>
        /// <param name="NewVersion"></param>
        public static void UpdateBizTalkProjVersion(string ProjFileName,string NewVersion)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(ProjFileName);
            XmlNode nodeProjCommon = xdoc.SelectSingleNode(CONST_XPath_ProjCommon);
            string strAsmFileVer = nodeProjCommon.Attributes.GetNamedItem("AssemblyFileVersion").Value;
            string strAsmInfoVer = nodeProjCommon.Attributes.GetNamedItem("AssemblyInformationalVersion").Value;
            string strAsmVer = nodeProjCommon.Attributes.GetNamedItem("AssemblyVersion").Value;

            nodeProjCommon.Attributes["AssemblyFileVersion"].Value = NewVersion;
            nodeProjCommon.Attributes["AssemblyInformationalVersion"].Value = NewVersion ;
            nodeProjCommon.Attributes["AssemblyVersion"].Value = NewVersion;

            xdoc.Save(ProjFileName);   
        }


    }
}
